const express = require("express");
const router = express.Router();
const db = require("../models");
const verify = require("../tools/verifyToken");

router.get("/all", verify, (req, res) => {
  console.log(req.ip);
  db.ContractPayment.findAll({ include: ["Contract", "manager"] })
    .then((contractPayments) => res.send(contractPayments))
    .catch((err) => {
      res.send(err);
    });
});

router.get("/:id", verify, (req, res) => {
  db.ContractPayment.findAll({
    where: {
      id: req.params.id,
    },
  })
    .then((contractPayment) => {
      res.send(contractPayment);
    })
    .catch((err) => {
      res.send(err);
    });
});

router.post("/add", verify, (req, res) => {
  const contractPayment = req.body;
  console.log(contractPayment);
  db.ContractPayment.create(contractPayment)
    .then((contractPayment) => {
      res.send(contractPayment);
    })
    .catch((err) => {
      res.send(err);
    });
});

router.post("/delete", verify, (req, res) => {
  db.ContractPayment.destroy({
    where: {
      id: req.body.id,
    },
  })
    .then((n) => {
      res.send({ deleted: n });
    })
    .catch((err) => {
      res.send(err);
    });
});

router.post("/edit", verify, (req, res) => {
  db.ContractPayment.update(req.body, {
    where: {
      id: req.body.id,
    },
  })
    .then((n) => {
      res.send({ updated: n });
    })
    .catch((err) => {
      res.send(err);
    });
});
module.exports = router;
